/*
 * FavorableLayout.java
 * business
 *
 * Created by ChenTao on 2017/2/21.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import com.yidiandao.business.R;
import com.yidiandao.business.utils.UIUtils;

import java.util.List;

/**
 * 店铺、商品优惠活动的Layout
 */
public class FavorableLayout extends ViewGroup {

    /**
     * 垂直间隔,默认15dp
     */
    private int mVerticalSpacing = UIUtils.dp2px(15);

    public FavorableLayout(Context context) {
        super(context);
    }

    public FavorableLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int widthSize = resolveSize(0, widthMeasureSpec);

        int paddingTop = getPaddingTop();
        int paddingBottom = getPaddingBottom();

        // 开始计算高度 需要加上paddingTop
        int childTop = paddingTop;

        View childView;
        for (int i = 0; i < getChildCount(); i++) {
            childView = getChildAt(i);
            // 测量 Child
            measureChild(childView, widthMeasureSpec, heightMeasureSpec);
            // 获取Child的高度
            int childHeight = childView.getMeasuredHeight();

            if (i < getChildCount() - 1) {
                childTop += childHeight + mVerticalSpacing;
            } else {
                childTop += childHeight;
            }
        }

        // 最后加上paddingBottom
        int heightSize = childTop + paddingBottom;

        setMeasuredDimension(widthSize, resolveSize(heightSize, heightMeasureSpec));
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

        int paddingLeft = getPaddingLeft();
        int paddingTop = getPaddingTop();

        int childLeft = paddingLeft;
        int childTop = paddingTop;

        View childView;
        for (int i = 0; i < getChildCount(); i++) {
            childView = getChildAt(i);

            if (childView.getVisibility() == GONE) {
                continue;
            }

            int childWidth = childView.getMeasuredWidth();
            int childHeight = childView.getMeasuredHeight();

            childView.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);

            childTop = childTop + childHeight + mVerticalSpacing;
        }
    }

    /**
     * 给Layout设置数据
     */
    public void setData(
            List<String> activeData,
            List<String> redPaperData,
            List<String> limitBuy,
            List<String> free,
            List<String> secondKill,
            List<String> limitGoods) {
        removeAllViews();
        if (activeData != null && activeData.size() != 0) {
            for (int i = 0; i < activeData.size(); i++) {
                FavorableView activeView = new FavorableView(getContext());
                activeView.setStyle(FavorableView.STYLE_ACTIVE);
                activeView.setDetail(UIUtils.getString(R.string.active_style), activeData.get(i));
                addView(activeView);
            }
        }

        if (redPaperData != null && redPaperData.size() != 0) {
            for (int i = 0; i < redPaperData.size(); i++) {
                FavorableView activeView = new FavorableView(getContext());
                activeView.setStyle(FavorableView.STYLE_RED_PAPER);
                activeView.setDetail(
                        UIUtils.getString(R.string.red_packet_style), redPaperData.get(i));
                addView(activeView);
            }
        }

        if (limitBuy != null && limitBuy.size() != 0) {
            for (int i = 0; i < limitBuy.size(); i++) {
                FavorableView activeView = new FavorableView(getContext());
                activeView.setStyle(FavorableView.STYLE_LIMIT_BUY);
                activeView.setDetail(UIUtils.getString(R.string.limitBuy_style), limitBuy.get(i));
                addView(activeView);
            }
        }
        if (free != null && free.size() != 0) {
            for (int i = 0; i < free.size(); i++) {
                FavorableView activeView = new FavorableView(getContext());
                activeView.setStyle(FavorableView.STYLE_FREE);
                activeView.setDetail(UIUtils.getString(R.string.free_style), free.get(i));
                addView(activeView);
            }
        }
        if (secondKill != null && secondKill.size() != 0) {
            for (int i = 0; i < secondKill.size(); i++) {
                FavorableView activeView = new FavorableView(getContext());
                activeView.setStyle(FavorableView.STYLE_SECOND_KILL);
                activeView.setDetail(
                        UIUtils.getString(R.string.secondKill_style), secondKill.get(i));
                addView(activeView);
            }
        }
        if (limitGoods != null && limitGoods.size() != 0) {
            for (int i = 0; i < limitGoods.size(); i++) {
                FavorableView activeView = new FavorableView(getContext());
                activeView.setStyle(FavorableView.STYLE_ACTIVE);
                activeView.setDetail(UIUtils.getString(R.string.limit_goods), limitGoods.get(i));
                addView(activeView);
            }
        }
    }
}
